package com.lin.controller;

import com.lin.VO.ResultVO;
import com.lin.dataobject.dao.ProductDao;
import com.lin.dto.OrderDTO;
import com.lin.dto.ProductDTO;
import com.lin.enums.ResultEnum;
import com.lin.exception.SellException;
import com.lin.server.OrderMasterService;
import com.lin.utils.ResultVOUtils;
import com.lin.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by MYQ on 2017/9/4.
 */
@Controller
@RequestMapping("/order")
@Slf4j
public class SellerOrderController {

    @Autowired
    private OrderMasterService orderMasterService;

    @RequestMapping("/list")
    public ModelAndView list(){
        return new ModelAndView("order/index1");
    }

    @ResponseBody
    @RequestMapping("/getData")
    public Map<String,Object> getData(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "rows", defaultValue = "10") Integer rows,
                                      HttpServletRequest request) {
        PageRequest pageRequest=new PageRequest(page-1,rows);
        String startTime=request.getParameter("startTime");
        String endTime=request.getParameter("endTime");
        String orderId=request.getParameter("orderId");
        String buyerName=request.getParameter("buyerName");
        String buyerPhone=request.getParameter("buyerPhone");
        String payStatus=request.getParameter("payStatus");
        String orderStatus=request.getParameter("orderStatus");
        Page<OrderDTO> orderDTOS= orderMasterService.findAll(startTime,endTime,orderId,buyerName,buyerPhone,payStatus,orderStatus,pageRequest);
        Map<String,Object> map=new HashMap<>();
        map.put("rows",orderDTOS.getContent());
        map.put("total", orderMasterService.getSize(startTime,endTime,orderId,buyerName,buyerPhone,payStatus,orderStatus));
        return map;
    }

    @RequestMapping("/view")
    public ModelAndView view(@RequestParam(value = "id",required = false) String id,
                             Map<String,Object> map){
        if (StringUtils.isNotEmpty(id)){
            OrderDTO orderDTO= orderMasterService.findOne(id);
            map.put("order",orderDTO);
        }
        return new ModelAndView("order/view");
    }

    /**
     * 取消订单
     * @param orderId
     * @return
     */
    @GetMapping("/cancel")
    @ResponseBody
    public ResultVO cancel(@RequestParam("orderId") String orderId,
                           Map<String, Object> map) {
        try {
            OrderDTO orderDTO = orderMasterService.findOne(orderId);
            orderMasterService.cancel(orderDTO);
        } catch (SellException e) {
            log.error("【卖家端取消订单】发生异常{}", e);
            return ResultVOUtils.error(ResultEnum.ORDER_CANCEL_ERROR.getCode(),
                    ResultEnum.ORDER_CANCEL_ERROR.getMessage());
        }
        return ResultVOUtils.success("订单取消成功");
//        map.put("msg", ResultEnum.ORDER_CANCEL_SUCCESS.getMessage());
//        map.put("url", "/sell/seller/order/list");
//        return new ModelAndView("common/success");
    }

    /**
     * 订单详情
     * @param orderId
     * @param map
     * @return
     */
    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam("orderId") String orderId,
                               Map<String, Object> map) {
        OrderDTO orderDTO = new OrderDTO();
        try {
            orderDTO = orderMasterService.findOne(orderId);
        }catch (SellException e) {
            log.error("【卖家端查询订单详情】发生异常{}", e);
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/order/list");
            return new ModelAndView("common/error", map);
        }

        map.put("orderDTO", orderDTO);
        return new ModelAndView("order/detail", map);
    }

    /**
     * 完结订单
     * @param orderId
     * @param map
     * @return
     */
    @GetMapping("/finish")
    @ResponseBody
    public ResultVO finish(@RequestParam("orderId") String orderId,
                                 Map<String, Object> map) {
        try {
            OrderDTO orderDTO = orderMasterService.findOne(orderId);
            orderMasterService.finish(orderDTO);
        } catch (SellException e) {
            log.error("【卖家端完结订单】发生异常{}", e);
            return ResultVOUtils.error(ResultEnum.ORDER_FINISH_ERROR.getCode(),
                    ResultEnum.ORDER_FINISH_ERROR.getMessage());
        }
        return ResultVOUtils.success("订单完成成功");
    }
}
